#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
from python_learn.obs_api import obs_post, obs_get


class ProjectRelate:
    def __init__(self):
        self.packages = ['python-flask', 'python-flask-restful', 'python-flask-restx']
        self.target_project = "home:Lhw-KunPeng:test:branches:openEuler:Mainline"
        self.repository = "standard_aarch64"
        self.arch = "aarch64"

    def create_link_project(self, target_project=None):
        """
        从主工程拉取某些包到私人工程
        :return:
        """
        target_project = self.target_project if target_project is None else target_project
        for package in self.packages:
            data = {"cmd": "branch", "target_project": target_project}
            result = obs_post(f'source/openEuler:Mainline/{package}', data=data)
            if result is None:
                print(f'{package} create failed')
            else:
                print(f'{package} create success')

    def get_project_packages(self, project=None):
        """
        获取工程下所有的软件包
        :return:
        """
        project = self.target_project if project is None else project
        result = obs_get(url=f'source/{project}')
        # print(result)
        return result

    def get_project_build_result(self, project=None):
        """
        获取工程的编译结果及工程下软件包的编译结果
        :return:
        """
        project = self.target_project if project is None else project
        result = obs_get(url=f'build/{project}/_result')
        print(result)

    def get_project_info(self, project=None):
        """
        获取工程基本信息，可用于判断工程是否存在
        :return:
        """
        project = self.target_project if project is None else project
        result = obs_get(url=f'source/{project}/_meta')
        print(result)

    def copy_project(self, project=None):
        """
        拷贝一个工程到另一个工程
        :param project: 拷贝的源工程
        :return:
        """
        project = self.target_project if project is None else project
        data = {"cmd": "copy", "oproject": project, "nodelay": True}
        result = obs_post(url=f'/source/home:Lhw-KunPeng:copy', data=data)
        # result = obs_get(url=f'/source/{project}/search')
        print(result)

    def project_test(self):
        data = {"target_project": "home:Lhw-KunPeng:copy:branches:openEuler:Mainline"}
        result = obs_post(url=f'/source', data=data)
        print(result)


if __name__ == '__main__':
    project_instance = ProjectRelate()
    # project_instance.create_link_project()

    # project_instance.get_project_packages()

    # project_instance.get_project_build_result()

    # project_instance.copy_project()
    # project_instance.project_test()

    # project_instance.get_project_info()
